<template>
  <div>
    <span style="color: red">表顺序: 从大到小删除,从小到大保存</span>
    <el-table v-loading="loading" :data="dataSource" border :height="autoHeight">
      <el-table-column prop="jdbcName" label="表名" align="center"></el-table-column>
      <el-table-column prop="jdbcComment" label="表备注" align="center">
        <template slot-scope="scope">
          <el-input v-model="scope.row[scope.column.property]" />
        </template>
      </el-table-column>
      <el-table-column prop="jdbcAlias" label="表别名" align="center">
        <template slot-scope="scope">
          <el-input v-model="scope.row[scope.column.property]" />
        </template>
      </el-table-column>
      <el-table-column prop="jdbcOrder" label="表顺序" align="center">
        <template slot-scope="scope">
          <el-input-number v-model="scope.row[scope.column.property]" controls-position="right"></el-input-number>
        </template>
      </el-table-column>
      <el-table-column prop="mainTable" label="主表" align="center">
        <template slot-scope="scope">
          <el-switch v-model="scope.row[scope.column.property]"></el-switch>
        </template>
      </el-table-column>
      <el-table-column prop="abilitiyList" label="表能力" align="center">
        <template slot-scope="scope">
          <el-select v-model="scope.row[scope.column.property]" placeholder="表能力" multiple style="width: 100%">
            <el-option v-for="item in abilities" :value="item.value" :label="item.label" :key="item.value" />
          </el-select>
        </template>
      </el-table-column>
      <el-table-column align="center" label="操作" width="120px">
        <template slot-scope="scope">
          <el-button type="danger" icon="el-icon-delete" circle @click="removeTable(scope.row, scope.$index)" title="删除"></el-button>
        </template>
      </el-table-column>
    </el-table>
    <div class="lab-table-page">
      <div style="display: inline-flex">
        <el-pagination
            :disabled="loading"
            :current-page="pageInfo.page"
            :page-sizes="[5, 10, 15, 20, 50, 100]"
            :page-size="pageInfo.pageSize"
            layout="total, sizes, prev, pager, next, jumper"
            :total="pageInfo.total"
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
        />
        <el-button-group style="padding: 2px 5px">
          <el-button icon="el-icon-refresh" title="刷新" :disabled="loading" @click="loadSource([])" />
        </el-button-group>
      </div>
    </div>
  </div>
</template>

<script>
import IdeaTable from "@/mixins/IdeaTable";

export default {
  name: "ModelTable",
  mixins: [IdeaTable],
  inject: {
    modelInfo: {
      type: 'modelInfo',
      default: function () {
        return []
      }
    },
    abilities: {
      from: 'abilities',
      default: function () {
        return []
      }
    }
  },
  watch: {
    'modelInfo.tables'(){
      this.initSource()
    }
  },
  computed: {
    autoHeight(){
      return `calc(100vh - 403px)`
    }
  },
  created() {
    this.rowKey = 'tableId'
    this.pageInfo.pageSize = 10
    this.initSource()
  },
  methods: {
    initSource() {
      this.dataSource.splice(0, this.dataSource.length)
      this.pageInfo.total = 0
      const { tables } = this.modelInfo
      if (tables && tables.length > 0) {
        this.pageInfo.total = tables.length
        tables.forEach(item => this.dataSource.push(item))
      }
    },
    removeTable(row, index) {
      this.dataSource.splice(index, 1)
      this.modelInfo.tables.splice(index, 1)
      if (this.modelInfo.fields) {
        this.modelInfo.fields = this.modelInfo.fields.filter(item => item.jdbcTable !== row.jdbcName)
      }
      if (this.modelInfo.joins) {
        this.modelInfo.joins = this.modelInfo.joins.filter(item => {
          if (item.leftTable === row.jdbcName || item.rightTable === row.jdbcName) {
            return false
          }
          return true
        })
      }
    }
  }
}
</script>

<style scoped>

</style>
